package com.leyou.item.controller;

import com.leyou.item.pojo.Category;
import com.leyou.item.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
@RequestMapping("/category")
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 根据父节点的id查询子节点集合列表
     * @param pid
     * @return
     */
    @GetMapping("/list")
    public ResponseEntity<List<Category>> queryCategoriesByPid(@RequestParam(value = "pid", defaultValue = "0") Long pid) {
        //400：参数非法
        if (pid == null || pid < 0) {
            return ResponseEntity.badRequest().build();
        }
        //调用方法
        List<Category> categories = categoryService.queryCategoriesByPid(pid);
        //判断是否能查询到
        if (CollectionUtils.isEmpty(categories)) {
            //404：资源服务器未找到
            return ResponseEntity.notFound().build();
        }
        //查询的到则返回集合
        return ResponseEntity.ok(categories);
    }

    /**
     * 通过品牌id查询中间表获得商品分类列表
     * @param bid
     * @return
     */
    @GetMapping("bid/{bid}")
    public ResponseEntity<List<Category>> queryByBrandId(@PathVariable("bid") Long bid){
        List<Category> list=categoryService.queryByBrandId(bid);
        //进行判断
        if(CollectionUtils.isEmpty(list)||list.size()<1){
            return ResponseEntity.status(HttpStatus.NOT_FOUND).build();
        }
        return ResponseEntity.ok(list);
    }
    /**
     * 通过ids查询商品分类名
     * @param ids
     * @return
     */
    @GetMapping
    public ResponseEntity<List<String>> queryNamesByIds(@RequestParam("ids")List<Long> ids){
        List<String> list = categoryService.queryNamesByIds(ids);
        if(CollectionUtils.isEmpty(list)){
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(list);
    }
}